package com.suqiu.qqrobot.service.impl;


import com.suqiu.qqrobot.common.log.annotation.LogMgr;
import com.suqiu.qqrobot.config.properties.OpenAiProperties;
import com.suqiu.qqrobot.model.Message;
import com.suqiu.qqrobot.model.TextMessage;
import com.unfbx.chatgpt.OpenAiClient;
import com.unfbx.chatgpt.entity.completions.Completion;
import com.unfbx.chatgpt.entity.completions.CompletionResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@LogMgr
@RequiredArgsConstructor
@Service("openAiDavinciModelService")
public class OpenAiDavinciModelService implements OpenAiChatService<String, String> {

    private final OpenAiClient openAiClient;
    private final OpenAiProperties openAiProperties;

    @Override
    public Message<String> chat(String sessionId, String question) {
        Completion completion = Completion.builder()
                .prompt(question)
                .maxTokens(openAiProperties.getChatGpt().getMaxToken())
                .temperature(openAiProperties.getChatGpt().getTemperature())
                .topP(openAiProperties.getChatGpt().getTopP())
                .build();
        CompletionResponse completions = openAiClient.completions(completion);
        Message<String> message = new TextMessage();
        message.setMessage(completions.getChoices()[0].getText());
        return message;
    }
}
